/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.inventario.util;

import br.com.inventario.dao.InventarioDAOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 *
 * @author lucas
 */
public class FabricaConexoes {
 private static final String DRIVER = "org.postgresql.Driver";
    private static final String URL = "jdbc:postgresql://localhost:5432/inventario";
    private static final String USER = "postgres";
    private static final String PASSWORD = "123456";
    
    public static Connection getConnection() throws InventarioDAOException{
        
        try{
            Class.forName(DRIVER);
            return DriverManager.getConnection(URL, USER, PASSWORD);
        }catch (Exception e){
            throw new InventarioDAOException(e.getMessage());
        }
    }
    
    public static void fecharConexao(Connection conexao, Statement stmt,
            ResultSet result) throws InventarioDAOException{
        fechar(conexao, stmt, result);
    }
    
    private static void fechar(Connection conexao, Statement stmt,
            ResultSet result) throws InventarioDAOException{
        try{
            if(result != null)
                result.close();
            if(stmt != null)
                stmt.close();
            if(conexao != null)
                conexao.close();
        }catch (Exception e){
            throw new InventarioDAOException(e.getMessage());
        }
    }
    
    public static void fecharConexao(Connection conexao, Statement stmt)
            throws InventarioDAOException{
        fechar(conexao, stmt, null);
    }
    
}
